<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false" id="salaryadjustContent">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息">
            <a-row :gutter="48">
              <a-col :span="8" :hidden="true">
                <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bpmBizTitle">
                  <a-input
                    :disabled="formDisabled"
                    v-model="model.bpmBizTitle"
                    placeholder="请输入标题"
                    :max-length="255"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
                  <select-user-by-dep :disabled=formDisabled  :multi="false"
                  v-model="model.employeeId" disabled></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="人员编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeNo">
                  <a-input :disabled="formDisabled" v-model="model.employeeNo" placeholder="请输入人员编号" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所在部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departmentId">
                  <j-select-depart :disabled="formDisabled" v-model="model.departmentId" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
                  <select-team-input :disabled="formDisabled" v-model="model.teamId" :tdlx="3" disabled/>
                </a-form-model-item>
              </a-col>
              <!-- <a-col :span="8">
                <a-form-model-item label="所在单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workUnit">
                  <j-select-depart :disabled="formDisabled" v-model="model.workUnit" multi disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所在部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departmentId">
                  <j-select-depart :disabled="formDisabled" v-model="model.departmentId" multi disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
                  <select-team-input :disabled="formDisabled" v-model="model.teamId" :tdlx="3" disabled/>
                </a-form-model-item>
              </a-col> -->
              <a-col :span="8">
                <a-form-model-item label="岗位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionId">
                  <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.positionId"
                                     dictCode="sys_position,name,id,del_flag = 0 and audit_status=2" placeholder="请选择岗位名称" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="调整前薪级"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="beforeSalaryScale"
                >
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.beforeSalaryScale"
                    dictCode="erp_pay_grade,salary_grade,id,del_flag = 0 and audit_status=2 order by sort"
                    placeholder="请选择调整前薪级"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="调整前薪资" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beforeSalary">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    v-model="model.beforeSalary"
                    placeholder="请输入调整前薪资"
                    style="width: 100%"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="生效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="effectiveDate">
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择生效日期"
                    v-model="model.effectiveDate"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="调整后薪级"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="afterSalaryScale"
                >
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.afterSalaryScale"
                    dictCode="erp_pay_grade,salary_grade,id,del_flag = 0 and audit_status=2 order by sort"
                    placeholder="请选择调整后薪级"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="调整后薪资" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="afterSalary">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    v-model="model.afterSalary"
                    placeholder="请输入调整后薪资"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="调整原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustRemark">
                  <a-textarea
                    :disabled="formDisabled"
                    v-model="model.adjustRemark"
                    rows="4"
                    placeholder="请输入调整原因"
                    :max-length="500"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="调岗信息">
            <a-row :gutter="48">
              <!-- <a-col :span="8">
                <a-form-model-item label="调入单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustUnit">
                  <j-select-depart :disabled="formDisabled" v-model="model.adjustUnit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="调入部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustDepart">
                  <j-select-depart :disabled="formDisabled" v-model="model.adjustDepart"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="调入团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustTeam">
                  <select-team-input :disabled="formDisabled" v-model="model.adjustTeam" :tdlx="3" />
                </a-form-model-item>
              </a-col> -->
              <a-col :span="8">
                <a-form-model-item label="调入岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustPosition">
                  <j-search-select
                      :disabled="formDisabled"
                      v-model="model.adjustPosition"
                      dict="sys_position,name,id,del_flag = 0 and audit_status=2"
                       placeholder="请选择调入岗位"
                    />
                  <!-- <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.adjustPosition"
                                     dictCode="sys_position,name,id,del_flag = 0 and audit_status=2" placeholder="请选择调入岗位"/> -->
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="公积金单位"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="providentFundUnit"
                >
                  <select-Company-input :disabled="formDisabled" v-model="model.providentFundUnit" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="参保单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="insuredUnit">
                  <select-Company-input :disabled="formDisabled" v-model="model.insuredUnit" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="参保基数（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="insuredBase">
                  <a-input :disabled="formDisabled" v-model="model.insuredBase" placeholder="请输入参保基数"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="公积金基数（元）"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="providentFundBase"
                >
                  <a-input
                    :disabled="formDisabled"
                    v-model="model.providentFundBase"
                    placeholder="请输入公积金基数"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                  <a-textarea :disabled="formDisabled" v-model="model.remark" rows="4" placeholder="请输入备注" :max-length="500" />
                </a-form-model-item>
              </a-col>
              <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
                <a-button @click="submitForm">提 交</a-button>
              </a-col> -->
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="3" header="附件信息">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
                  <j-upload :disabled="formDisabled" v-model="model.attachment"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
      </a-form-model>
      <common-flow-list v-if="formBpm && model.bpmStatus !== '1'" :formData="formData" style="margin-top: 10px"></common-flow-list>
    </j-form-container>
    <a-row :gutter="48">
      <a-col :span="24" style="text-align: center;margin-top: 15px">
        <a-button v-print="'#salaryadjustContent'" type="primary" v-if="formBpm" :style="{ marginRight: showFlowSubmitButton?'10px':'0px' }">打印</a-button>
        <a-button @click="submitForm('submit')" v-if="showFlowSubmitButton">提 交</a-button>
      </a-col>
    </a-row>
    <process-config-modal ref="processConfig" @ok="handleSuccess"></process-config-modal>
  </a-spin>
</template>

<script>
import { httpAction,postAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
import { mapGetters } from 'vuex';
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import ProcessConfigModal from '@/views/scy/components/ProcessConfigModal'
import JSearchSelect from '@/views/scy/components/JSearchSelect.vue'
import CommonFlowList from '@/views/components/CommonFlowList.vue';
export default {
  name: 'ErpHrPostSalaryAdjustmentForm',
  components: {
    SelectUserByDep,
    SelectTeamInput,
    SelectCompanyInput,
    ProcessConfigModal,
    JSearchSelect,
    CommonFlowList
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      activeKey:['1','2','3'],
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        positionId:[
          { required: true, message: '您未在当前部门下任职，请联系HR!'},
        ],
        teamId: [
          { required: true, message: '当前部门未找到关联的团队，请联系HR!'},
        ],
        bpmBizTitle: [
              { required: true, message: '请输入流程标题!'},
          ],
        effectiveDate: [
            { required: true, message: '请选择生效日期!'},
        ],
        afterSalaryScale: [
            { required: true, message: '请选择调整后薪级!'},
        ],
        afterSalary: [
            { required: true, message: '请选择调整后薪资!'},
        ],
        adjustRemark: [
            { required: true, message: '请输入调整原因!'},
        ],
        adjustUnit: [
            { required: true, message: '请选择调入单位!'},
        ],
        adjustDepart: [
            { required: true, message: '请选择调入部门!'},
        ],
        adjustTeam: [
            { required: true, message: '请选择调入团队!'},
        ],
        adjustPosition: [
            { required: true, message: '请选择调入岗位!'},
        ],
        providentFundUnit: [
            { required: true, message: '请选择公积金单位!'},
        ],
        providentFundBase: [
            { required: true, message: '请选择公积金基数!'},
        ],
        insuredUnit: [
            { required: true, message: '请选择参保单位!'},
        ],
        insuredBase: [
            { required: true, message: '请选择参保基数!'},
        ]
      },
      url: {
        startProcess: "/api/erpmng/act/process/extActProcess/startMutilProcess",
        add: '/api/erp/hr/adjust/erpHrPostSalaryAdjustment/add',
        edit: '/api/erp/hr/adjust/erpHrPostSalaryAdjustment/edit',
        queryById: '/api/erp/hr/adjust/erpHrPostSalaryAdjustment/queryById',
        queryByEmployeeId: '/api/erp/hr/adjust/erpHrPostSalaryAdjustment/querySalaryByEmployeeId',
        queryPostRecordByInfo:'/api/erp/files/employee/erpEmployeePostChangeRecord/queryPostRecordByInfo',
      },
      //代码生成后需手动设置流程编码
      flowCode: 'onl_erp_hr_post_salary_adjustment',
      formUrl: "erp/hr/adjust/modules/ErpHrPostSalaryAdjustmentForm",
      formUrlMobile: "",
      processId:"1540234473986088962",
      beanName:"erpHrPostSalaryAdjustmentServiceImpl"
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    ...mapGetters(['userInfo']),
    add() {
      let userinfo=this.userInfo();
      console.log(userinfo);
      this.$set(this.modelDefault,'employeeId',userinfo.empId);
      this.$set(this.modelDefault,'employeeNo',userinfo.employee.employeeNo);
      // this.$set(this.modelDefault,'workUnit',userinfo.employee.orgDepart);
      this.$set(this.modelDefault,'departmentId',userinfo.deptId);
      // this.$set(this.modelDefault,'teamId',userinfo.employee.teamId);
      this.$set(this.modelDefault,'entryTime',userinfo.employee.joinDate);
      this.$set(this.modelDefault,'beforeSalaryScale',userinfo.employee.payGradeId);
      this.$set(this.modelDefault,'bpmBizTitle',userinfo.employee.name);
      getAction(this.url.queryByEmployeeId,{ employeeId: userinfo.empId }).then(res => {
        if(res.success){
          let data=res.result;
          console.log(data);
          this.$set(this.modelDefault,'beforeSalary',data);
          this.edit(this.modelDefault)
        }
      })
      getAction(this.url.queryPostRecordByInfo,{ employeeId: userinfo.empId,departId:userinfo.deptId }).then(res => {
        console.log(res);
        if(res.success){
          let data=res.result;
          if(data){
            this.$set(this.model,'teamId',data[0].teamId);
            this.$set(this.model,'positionId',data[0].postId);
          }
        }
      })
    },
    edit(record) {
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    handleSuccess(){
      this.$emit('ok');
    },
    //发起流程
    startProcess: function (id) {
      // var that = this;
      // var param = {
      //   flowCode: that.flowCode,
      //   id: id,
      //   formUrl: that.formUrl,
      //   formUrlMobile: that.formUrlMobile
      // }
      // postAction(that.url.startProcess, param).then((res) => {
      //   if (res.success) {
      //     that.$message.success(res.message);
      //     that.$emit('ok');
      //   } else {
      //     that.$message.warning(res.message);
      //   }
      // }).catch((e) => {
      //   this.$message.warning('不识别的请求!');
      // });
      let record={id:id};
      this.$refs.processConfig.visible = true
      this.$refs.processConfig.formUrl = this.formUrl
      this.$refs.processConfig.formUrlMobile = this.formUrlMobile
      this.$refs.processConfig.flowCode = this.flowCode
      this.$refs.processConfig.edit(record,'',this.beanName);
    },
    //保存草稿
    submitTempForm() {
      const that = this
      // 触发表单验证
      //that.confirmLoading = true
      let httpurl = ''
      let method = ''
      if (!this.model.id) {
        httpurl += this.url.add
        method = 'post'
      } else {
        httpurl += this.url.edit
        method = 'put'
      }
      httpAction(httpurl, this.model, method)
        .then((res) => {
          if (res.success) {
            let data=res.result;
            that.$message.success(res.message)
            that.$emit('ok')
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          //that.confirmLoading = false
        })
        
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                let data=res.result;
                if (this.model.id) {
                  //that.$message.success(res.message)
                  this.startProcess(this.model.id);
                }else{
                  //that.$message.success(res.message)
                  this.startProcess(data);
                }
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
<style>
  @import "../../../../../utils/print.css";
</style>